package util;

import com.google.common.collect.AbstractIterator;

import java.util.Iterator;

/**
 * Created by Gerardo on 12/4/13.
 */
public class StringIterable implements Iterable<Character> {
    private final String string;

    public static StringIterable of(final String string) {
        return new StringIterable(string);
    }

    private StringIterable(final String string) {
        this.string = string;
    }

    @Override
    public Iterator<Character> iterator() {
        return new AbstractIterator<Character>() {
            private int i = 0;

            @Override
            protected Character computeNext() {
                if (i < string.length())
                    return string.charAt(i++);
                return endOfData();
            }
        };
    }
}
